package diona.model

import net.liftweb.squerylrecord.RecordTypeMode._
import org.squeryl.{Table, Schema}
import diona.search.{SearchTask, SearchItem, SearchResult, SearchRequest}
import pages.PageInfo

/**
 * User: anton
 * Date: 02.04.11 19:55
 */
object PublisherSchema extends Schema {

  val users: Table[User] = table[User]
  val images: Table[ImageInfo] = table[ImageInfo]
  val googleAuths: Table[GoogleAuth] = table[GoogleAuth]
  val dropboxAuths: Table[DropboxAuth] = table[DropboxAuth]

  val searchRequests: Table[SearchRequest] = table[SearchRequest]
  on(searchRequests)(t => declare(t.query.~is(indexed)))

  val searchResults: Table[SearchResult] = table[SearchResult]
  val searchItems: Table[SearchItem] = table[SearchItem]
  val searchTasks: Table[SearchTask] = table[SearchTask]

  val pageInfos: Table[PageInfo] = table[PageInfo]

  val userToImages = oneToManyRelation(users, images).via(_.id === _.userId)
  val userToGoogleAuth = oneToManyRelation(users, googleAuths).via(_.id === _.userId)
  val userToDropboxAuth = oneToManyRelation(users, dropboxAuths).via(_.id === _.userId)
  val userToSearchRequests = oneToManyRelation(users, searchRequests).via(_.id === _.userId)
  val searchRequestToResults = oneToManyRelation(searchRequests, searchResults).via(_.id === _.requestId)

  val searchItemToResults = oneToManyRelation(searchResults, searchItems).via(_.id === _.resultId)
  val searchItemToRequests = oneToManyRelation(searchRequests, searchItems).via(_.id === _.requestId)
  val searchItemToPageInfo = oneToManyRelation(pageInfos, searchItems).via(_.id === _.pageId)
  val searchItemToTask = oneToManyRelation(searchTasks, searchItems).via(_.id === _.taskId)

  val searchTaskToRequest = oneToManyRelation(searchRequests, searchTasks).via(_.id === _.requestId)
  val searchTaskToResult = oneToManyRelation(searchResults, searchTasks).via(_.id === _.resultId)

  override def drop
  {
    super.drop
  }
}